﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.IO;

namespace OINS
{
    public class Resources
    {
        //  Vector containing the list of files, we share.
        public List<Record> ListOfFiles=new List<Record>();

       //Method to update list of files by adding new folder
        public void AddFolderToResources(String Director)
        {
            DirectoryInfo dir = new DirectoryInfo(Director);
            DirectoryInfo[] directories = dir.GetDirectories();
            if (directories != null)
            {
                FileInfo[] filesInDir;
                try
                {
                    filesInDir = dir.GetFiles();
                }
                catch (UnauthorizedAccessException)           
                {
                    filesInDir = null;
                }
                foreach (FileInfo file in filesInDir)
                {
                    ListOfFiles.Add(new Record(file.Name, file.Extension, file.DirectoryName));
                }
                Console.WriteLine(dir.Name);
                foreach (DirectoryInfo newDir in directories)
                {
                    String a=newDir.Attributes.ToString();
                    
                   

                    AddFolderToResources(Path.Combine(Director,newDir.Name));
                }
            }
        }
        
        public void ShowList()
        {
            foreach(Record wpis in ListOfFiles)
            {
                Console.WriteLine(" Nazwa :{0} Typ: {1}",wpis.FileName,wpis.Extension);
            }
        }
        public void SaveTheList()
        {
            FileInfo saveTheList = new FileInfo("list.txt");
            StreamWriter stream = new StreamWriter("list.txt",false);
            foreach (Record wpis in ListOfFiles)
                stream.WriteLine(wpis.FileName + "\t" + wpis.Extension+"\t"+wpis.Directory);
            stream.Close();
        }
        public void LoadTheList()
        {
            
            StreamReader stream;
    
            
                FileInfo list = new FileInfo("list.txt");
                if (list.Exists)
                    stream = list.OpenText();
                else
                {
                    list.Create();
                    
                        stream = list.OpenText();
                }

                
           
            String line;
            ListOfFiles.Clear();
            do
            {
                line = stream.ReadLine();
                if (line != null)
                {
                    String[] temp = line.Split('\t');
                    ListOfFiles.Add(new Record(temp[0], temp[1],temp[2]));
                }
                else
                    continue;
            } while (line != null);
            stream.Close();
        }
    }
}
